<?php

class SettingsController extends Zend_Controller_Action {

    public function preDispatch() {
        $session = new Zend_Session_Namespace('UnCMS');
        if (!isset($session->acl->settings))
            $this->_redirect(SITE_URL);
        $t = Zend_Registry::get('Zend_Locale');
        //Отрубаем шаблоны, если это AJAX запрос
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
        }
        $this->view->navigation_line = array($t->_('System settings') => ADMIN_URL . '/settings');
    }

    public function indexAction() {
        $model = new SettingsModel();
        $forms = new SettingsForm();
        $form = $forms->MainSettings();
        $this->view->form = $form;

        if ($this->_request->isPost()) {//Форма отправлена
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                array_pop($formData);
                $model->updateSettings($formData);
                $this->_redirect('/settings');
            } else {
                $form->populate($formData);
            }
        } else {
            $settings = $model->getSettings();
            $form->populate($settings);
        }
    }

    public function moduleAction() {
        $controller = ucfirst($this->getRequest()->getParam('name')) . 'Controller';
        if (method_exists($controller, 'settingsAction'))
            $this->_forward('settingsAction', $controller);
    }

    /*
     * Создание резервной копии
     */

    public function backupsAction() {
        $this->view->navigation_line = $this->view->navigation_line + array($this->view->t->_('Backups') => ADMIN_URL . '/settings/backups');

        $model = new SettingsModel();
        $action = $this->getRequest()->getParam('a');
        $time = (int) $this->getRequest()->getParam('time');

        if ($action == 'restore' && $time > 0) {
            if ($model->restoreDB($time))
                $this->_redirect(ADMIN_URL . '/settings/backups');
        }

        if ($action == 'backup') {
            $backup = $model->backupDB();
            $handle = fopen(APP_PATH . '/backups/' . time() . '.sql', 'x+');
            fwrite($handle, $backup);
            fclose($handle);
            $this->_redirect(ADMIN_URL . '/settings/backups');
        }

        if ($action == 'remove' && $time > 0) {
            if (file_exists(APP_PATH . '/backups/' . $time . '.sql')) {
                unlink(APP_PATH . '/backups/' . $time . '.sql');
            }
            $this->_redirect(ADMIN_URL . '/settings/backups');
        }

        //Получаем список бэкапов
        $this->view->backups = $model->getBackups();
    }

    public function updateAction() {
        $this->view->navigation_line = $this->view->navigation_line + array($this->view->t->_('Update') => ADMIN_URL . '/settings/update');
        $action = $this->getRequest()->getParam('a');
        if ($action == 'start') {
            $model = new SettingsModel();
            $result = $model->updateSystem($this->view->config->version, $this->view->config->update);
            if ($result) {
                $this->view->ok = true;
            } else {
                $this->view->ok = false;
            }
        }
    }

}
